package com.sinog.biz.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @desc redis配置类
 * @author lzq
 * @date 2021/11/11 13:43
 */
@Configuration
@ConfigurationProperties(prefix ="spring.buredis")
@Slf4j
public class RedisConfig {

    @Value("${spring.buredis.host}")
    private String host;
    @Value("${spring.buredis.port}")
    private int port;
    @Value("${spring.buredis.timeout}")
    private int timeout;
    @Value("${spring.buredis.jedis.pool.max-idle}")
    private int maxIdle;
    @Value("${spring.buredis.jedis.pool.max-wait}")
    private long maxWaitMillis;
    @Value("${spring.buredis.password}")
    private String password;

    /**
     * 描述
     * @return JedisPool
     */
    @Bean
    public JedisPool redisPoolFactory() {
        log.info("JedisPool注入成功！！");
        log.info("buredis地址：" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        // 是否启用pool的jmx管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(true);
        return new JedisPool(jedisPoolConfig,host,port,timeout,password);
    }
}